/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.seari.bean.Role;
import com.seari.bean.RolePrivilege;
import com.seari.enums.RoleTypeEnums;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryRole;
import com.seari.service.RolePrivilegeService;
import com.seari.service.RoleService;
import com.seari.utils.StringHelper;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("role/")
public class RoleController
{
    @Autowired
    private RoleService roleService;
    
    @Autowired
    private RolePrivilegeService userRolePrivilegeService;
    
   /* @Autowired
    private UserPrivilegeService userPrivilegeService;*/
    
    
    @RequestMapping("getRoleByParams")
    @ResponseBody
    public DataGridBean<Role> getRoleByParams(HttpServletRequest req)
    {
        DataGridBean<Role> grid = new DataGridBean<Role>();
        String currPage = req.getParameter("page");
        String pageSize = req.getParameter("limit");
    	String match = req.getParameter("match");
        QueryRole qur = new QueryRole();
        if(StringUtils.isNotBlank(currPage) && StringUtils.isNotBlank(pageSize))
        {
        	qur.setStart((Integer.parseInt(currPage) - 1) * Integer.parseInt(pageSize));
        	qur.setLimit(Integer.parseInt(pageSize));
        }
        List<Role> userRoles = null;
        if(StringUtils.isNotBlank(match)){
        	qur.setMatch(match);
        	userRoles = roleService.roleSearch(qur);
        	Integer count = roleService.roleSearchCount(qur);
        	grid.setRows(userRoles);
            grid.setTotal(count);
            return grid;
            
        }
        userRoles = roleService.getRoleByParams(qur);
        Integer count = roleService.getRoleCount();
        grid.setRows(userRoles);
        grid.setTotal(count);
        return grid;
    }
    
    
    @RequestMapping("getRoleByUrid")
    @ResponseBody
    public DataGridBean<Role> getRoleByUrid(HttpServletRequest req)
    {
    	DataGridBean<Role> grid = new DataGridBean<Role>();
    	 QueryRole qur = new QueryRole();
    	String urid = req.getParameter("urid");
    	
        {
        	qur.setUrid(Integer.parseInt(urid));
        	List<Role> userRoles = roleService.getRoleByUrid(qur);
            grid.setRows(userRoles);
            grid.setTotal(1);
           
        }

        return grid;
    }
    
    @RequestMapping("addRole")
    @ResponseBody
    public MessageBean addRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String roleName = req.getParameter("roleName");
        String roleCode = req.getParameter("roleCode");
        String roleDescribe = req.getParameter("roleDescribe");
        // 权限菜单集合
        String[] values = req.getParameterValues("mpids[]");
        
        if(StringUtils.isBlank(roleName) 
        		|| StringUtils.isBlank(roleCode) 
        		|| StringUtils.isBlank(roleDescribe) 
        		|| ArrayUtils.isEmpty(values))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        QueryRole qur = new QueryRole();
        qur.setRoleName(roleName);
        //检查是否已存在此角色名
        int resultNum = roleService.checkRoleExist(qur);
        if(resultNum > 0)
        {
            
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("已存在此角色名");
        }else
        {
        	Integer [] mpids = new Integer[values.length];
        	for (int i = 0; i < values.length; i++) {
        		mpids[i] = Integer.parseInt(values[i]);
			}
        	
            Role role = new Role();
            role.setRoleName(roleName);
            role.setRoleCode(roleCode);
            role.setRoleDescribe(roleDescribe);
            
            RolePrivilege rolePrivilege = new RolePrivilege();
            rolePrivilege.setMpids(mpids);
            
            int resultNum2 = roleService.addRole(role,rolePrivilege);
            if(resultNum2 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色添加失败");
                return mb;
            }
            
        }
        return mb;
    }
    
    @RequestMapping("updateRole")
    @ResponseBody
    public MessageBean updateRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String urid = req.getParameter("urid");
        String roleName = req.getParameter("roleName");
        String roleCode = req.getParameter("roleCode");
        String roleDescribe = req.getParameter("roleDescribe");

        
        String[] values = req.getParameterValues("mpids[]");
        //String[] values = {"42","43"};
        
        if(StringUtils.isBlank(urid) 
        		|| StringUtils.isBlank(roleName)
        		|| StringUtils.isBlank(roleCode)
        		|| StringUtils.isBlank(roleDescribe)
        		|| ArrayUtils.isEmpty(values))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        
        QueryRole qur = new QueryRole();
        qur.setRoleName(roleName);
        qur.setUrid(Integer.parseInt(urid));
        int resultNum = roleService.checkRoleExist(qur);
        if(resultNum > 0)
        {
            //检查是否已存在此角色名
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("已存在此角色名");
        }else
        {
            Role role = new Role();
            role.setUrid(Integer.parseInt(urid));
            role.setRoleName(roleName);
            role.setRoleCode(roleCode);
            role.setRoleDescribe(roleDescribe);
            
            RolePrivilege rolePrivilege = new RolePrivilege();
            Integer [] mpids = new Integer[values.length];
            for (int i = 0; i < values.length; i++) {
				
            	mpids[i] = Integer.parseInt(values[i]);
            	
			}
            rolePrivilege.setUrid(Integer.parseInt(urid));
            rolePrivilege.setMpids(mpids);
            int resultNum2 = roleService.updateRole(role,rolePrivilege);
            if(resultNum2 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色修改失败");
                return mb;
            }
            else
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                    mb.setMessage("角色修改成功");
                }
            }
        return mb;
    }
    
    @RequestMapping("deleteRole")
    @ResponseBody
    public MessageBean deleteRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String urid = req.getParameter("urid");
        if(StringUtils.isBlank(urid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else
        {
            int resultNum = roleService.deleteRole(Integer.parseInt(urid));
            if(resultNum < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色删除失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("角色删除成功");
            }
        }
        return mb;
    }

    /**
     * 通过用户类型获取角色列表，管理员只能获取普通用户，超级管理员可以获取到所有角色
     * @param roleListStr 逗号分隔的角色code值
     * @return
     */
    @RequestMapping("getRoleListByUserType")
    @ResponseBody
    public DataGridBean<Role> getRoleListByUserType(String roleListStr)
    {
        DataGridBean<Role> result = new DataGridBean<Role>();
        String[] split = roleListStr.split(",");
        List<String> roleList = Collections.arrayToList(split);
        QueryRole queryRole = new QueryRole();
        queryRole.setStart(0);
        queryRole.setLimit(1000);
        if(roleList.indexOf(RoleTypeEnums.SUPER_ADMIN.getCode()) > -1){
        }else if(roleList.indexOf(RoleTypeEnums.ADMIN.getCode()) > -1){
            queryRole.setNeglectRoleCode(RoleTypeEnums.getAdminLevelRoles());
        }else{
            return result;
        }
        List<Role> resultRoleList = roleService.getRoleByParams(queryRole);
        result.setRows(resultRoleList);
        result.setTotal(resultRoleList.size());
        return result;
    }
}
